package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lixu
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    private DtsRoleMapper dtsRoleMapper;
    @Autowired
    private DtsAdminMapper dtsAdminMapper;

    //1:查询角色集合 根据用户的ID
    @Override
    public Set<String> findRolesByIds(Integer[] roleIds) {
        if (null != roleIds && roleIds.length > 0) {
            List<DtsRole> dtsRoleList = dtsRoleMapper.selectList(new QueryWrapper<DtsRole>()
                    .select("name")//
                    .in("id", roleIds)
                    .eq("deleted", false));
            if (!CollectionUtils.isEmpty(dtsRoleList)) {
                //偷天换日
                return dtsRoleList.stream().map(DtsRole::getName).collect(Collectors.toSet());
            }
        }
        return null;
    }

    /**
     * 查询角色列表   Mp
     * @return
     */
    @Override
    public List<CatVo> findRoleList() {

        return dtsRoleMapper.findRoleList();
    }

    //分页查询
    @Override
    public IPage<DtsRole> list(Integer page, Integer limit, String sort, String order, String rolename) {
        return dtsRoleMapper.selectPage(new Page<DtsRole>(page,limit),
                new QueryWrapper<DtsRole>()
             .like(!StringUtils.isEmpty(rolename),"name",rolename)
             .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }


    /**
     * 根据角色id数组返回对应的角色名称集合
     * @param roleIds
     * @return
     */
    @Override
    public Set<String> queryByIds(Integer[] roleIds) {
        Set<String> roles = new HashSet<String>();
        if (roleIds == null || roleIds.length == 0) {
            return roles;
        }

        //设置查询条件
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        wapper.in("id", Arrays.asList(roleIds));
        wapper.eq("deleted", false);
        wapper.eq("enabled", true);

        //查询
        List<DtsRole> roleList = dtsRoleMapper.selectList(wapper);

        for (DtsRole role : roleList) {
            roles.add(role.getName());
        }

        return roles;

    }

    /**
     * 分页查询
     */
    @Override
    public IPage<DtsRole> querySelective(String roleName, Integer page, Integer size, String sort, String order) {
        IPage<DtsRole> iPage = new Page<>(page, size);

        //设置查询条件
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            wapper.like("name", roleName);
        }
        wapper.eq("deleted", false);

        //设置排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                wapper.orderByAsc(sort);
            } else {
                wapper.orderByDesc(sort);
            }

        }

        //分页查询
        iPage = dtsRoleMapper.selectPage(iPage, wapper);

        return iPage;
    }

    /**
     * 根据角色id查询角色
     * @param id
     * @return
     */
    @Override
    public DtsRole findById(Integer id) {
        return dtsRoleMapper.selectById(id);
    }

    /**
     * 添加角色
     * @param role
     */
    @Override
    public void add(DtsRole role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        dtsRoleMapper.insert(role);
    }

    /**
     * 删除角色
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        dtsRoleMapper.deleteById(id);
    }

    /**
     * 根据角色id, 更新角色
     * @param role
     */
    @Override
    public void updateById(DtsRole role) {
        role.setUpdateTime(new Date());
        dtsRoleMapper.updateById(role);
    }

    /**
     * 根据角色名字判断是否有这个角色
     * @param name
     * @return
     */
    @Override
    public boolean checkExist(String name) {
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        wapper.eq("deleted", false);
        wapper.eq("name", name);
        Long count = dtsRoleMapper.selectCount(wapper);
        return count != 0;
    }

    /**
     * 查询所有角色
     * @return
     */
    @Override
    public List<DtsRole> queryAll() {
        QueryWrapper<DtsRole> wapper = new QueryWrapper<>();
        wapper.eq("deleted", false);
        return dtsRoleMapper.selectList(wapper);
    }
}
